package com.feeling.oafeeling.controller;

import com.feeling.oafeeling.common.pojo.AjaxResult;
import com.feeling.oafeeling.pojo.SysInstitution;
import com.feeling.oafeeling.pojo.SysInstitutionNode;
import com.feeling.oafeeling.service.SysInstitutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/institution")
public class SysInstitutionController {
    @Autowired
    private SysInstitutionService sysInstitutionService;

    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @PostMapping("/tree")
    public AjaxResult getInstitutionTree(@RequestBody SysInstitution sysInstitution) {
        System.out.println(sysInstitution);
        List<SysInstitutionNode> institutionTree = sysInstitutionService.getInstitutionTree(sysInstitution);
        return institutionTree == null ? AjaxResult.error("获取失败"):AjaxResult.success(institutionTree);
    }

    @PutMapping("/updata")
    public AjaxResult upDataInstitutionTree(@RequestBody SysInstitution sysInstitution) throws Throwable{
        System.out.println(sysInstitution);
        sysInstitutionService.upDataInstitution(sysInstitution);
        return AjaxResult.success();
    }

    //分页显示机构信息
    @GetMapping("/{currentPage}/{pageSize}")
    public AjaxResult getInstitutionByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize){
        List<SysInstitution> institutions = sysInstitutionService.getInstitutionByPage(currentPage, pageSize);
        if (institutions.isEmpty()){
            return AjaxResult.error("暂无机构数据");
        }
        return AjaxResult.success("查询成功",institutions);
    }

    //新建机构
    @PostMapping("/add")
    public AjaxResult addInstitutionTree(@RequestBody SysInstitution sysInstitution) throws Throwable {
        System.out.println(sysInstitution);
        sysInstitutionService.addInstitutionTree(sysInstitution);
        return AjaxResult.success();
    }

}
